// localstorage 保存数据
export const saveLocal = (key: string, value?: any) => {
  if(value == undefined){
    localStorage.removeItem(key)
    return ;
  }
  localStorage.setItem(key, JSON.stringify({ value }));
};

// 获取 localstorage 保存的数据 type 可以用来对数据做初步类型转换 目前仅支持 json对象转换
export function getLocal(key: string) {
  const data = localStorage.getItem(key);
  if (data == null) {
    return null;
  }
  try {
    const { value }: {value: any } = JSON.parse(data);
    return value;
  } catch (e) {
    return data;
  }
}

// 清空 localstorage
export const clearLocal = () => {
  localStorage.clear();
};
